import http from 'http'
import https from 'https'

export function httpGet(url: string, opts: any = {}): Promise<Buffer> {
  return new Promise<any>((resolve, reject) => {
    const req = http.get(url, opts, res => {
      let buf = Buffer.alloc(0)
      res.on('data', chunk => {
        buf = Buffer.concat([buf, chunk])
      })
      res.on('end', () => {
        resolve(buf)
      })
      res.on('error', reject)
    })
    req.on('error', reject)
    req.end()
  })
}

export function httpsGet(url: string, opts: any = {}): Promise<Buffer> {
  return new Promise<Buffer>((resolve, reject) => {
    const req = https.get(url, opts, res => {
      let buf = Buffer.alloc(0)
      res.on('data', chunk => {
        buf = Buffer.concat([buf, chunk])
      })
      res.on('end', () => {
        resolve(buf)
      })
      res.on('error', reject)
    })
    req.on('error', reject)
    req.end()
  })
}
